<template>
  <w-base ref="WBase" :size="size" :souce-url="souceUrl" />
</template>

<script lang="ts">
import { Vue, Component, Prop } from 'vue-property-decorator'
import WBase, { BaseImageUpType } from '@/components/global/imageUp/base'

@Component({
  name: 'ComGlobalImageUp',
  components: {
    WBase
  }
})
export default class ComGlobalImageUp extends Vue {
  @Prop({ default: '', type: String }) souceUrl!: string;
  @Prop({ default: '200px', type: String }) size!: string;

  upImage (require = false) {
    return new Promise((resolve, reject) => {
      (this.$refs.WBase as BaseImageUpType)
        .getImage()
        .then((data) => {
          if (data.type === 'SEL_FILE') {
            this.$wAxios
              .$upOss(data.event, process.env.OSS_IMAGE_PREFIX as any)
              .then((data: string) => {
                (this.$refs.WBase as BaseImageUpType).clear()
                resolve(data)
              })
              .catch((err: Error) => {
                reject(err)
              })
          } else {
            resolve(data.url)
          }
        })
        .catch((err) => {
          if (require) {
            this.$message.error(err.msg)
            reject(err)
          } else {
            resolve('')
          }
        })
    })
  }
}
</script>

<style lang="less" scoped></style>
